import 'package:flutter/material.dart';
import 'package:flowerny/splash.dart';
import 'layout_type.dart';
import 'package:flowerny/page/p_tabs/mession/page_mession.dart';
import 'package:flowerny/page/p_tabs/mall/page_good.dart';
import 'package:flowerny/page/p_tabs/reward/page_reward.dart';
import 'package:flowerny/page/p_tabs/mine/page_mine.dart';
import 'dart:io';
import 'package:flutter/services.dart';
void main(){
  runApp(MyApp());
  if (Platform.isAndroid) {
// 以下两行 设置android状态栏为透明的沉浸。写在组件渲染之后，是为了在渲染后进行set赋值，覆盖状态栏，写在渲染之前MaterialApp组件会覆盖掉这个值。
    SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
    );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}
const int ThemeColor = 0xFF00D7C6;
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Tiny',
      theme: ThemeData(
        primaryColor: Color(ThemeColor),
        accentColor: Color(0xFF888888),
        backgroundColor: Color(0xFFEFEFEF),
      ),
      home: SplashPage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>{
  List<Widget> _pages;
  PageController _controller;
  @override
  void initState() {
    super.initState();
    _pages = List() ..add(MessionPage())  ..add(GoodsPage())
      ..add(Reward())  ..add(MinePage());
    _controller = PageController(initialPage: 0);
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }


  LayoutType _layoutSelection = LayoutType.mession;
  Color _colorMatching(LayoutType type){
    return _layoutSelection == type?Color.fromARGB(255, 0, 215, 198):Colors.grey[900];
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _bulidBody(),
      bottomNavigationBar: _buildButtomNavBar(),
    );
  }

  Widget _bulidBody(){
    return PageView.builder(
        physics: NeverScrollableScrollPhysics(),//viewPage禁止左右滑动
        controller: _controller,
        itemCount: _pages.length,
        itemBuilder: (context, index) => _pages[index]);
  }

  Widget _buildButtomNavBar(){
    return BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      items: [
        _buildBarItem(
          icon: _layoutSelection == LayoutType.mession?
          'images/ic_main_tab_find_pre.png':'images/ic_main_tab_find_nor.png',
          layoutSelection: LayoutType.mession,
        ),
        _buildBarItem(
          icon: _layoutSelection == LayoutType.goods?
          'images/ic_main_tab_company_pre.png':'images/ic_main_tab_company_nor.png',
          layoutSelection: LayoutType.goods,
        ),
        _buildBarItem(
          icon: _layoutSelection == LayoutType.reward?
          'images/ic_main_tab_contacts_pre.png':'images/ic_main_tab_contacts_nor.png',
          layoutSelection: LayoutType.reward,
        ),
        _buildBarItem(
          icon: _layoutSelection == LayoutType.mine?
          'images/ic_main_tab_my_pre.png':'images/ic_main_tab_my_nor.png',
          layoutSelection: LayoutType.mine,
        ),
      ],
      onTap: _onSelectTab,
    );
  }

  BottomNavigationBarItem  _buildBarItem(
      {String icon,LayoutType layoutSelection}){
    String text = layoutName(layoutSelection);
    return BottomNavigationBarItem(
        icon: Image.asset(
          icon,
          width: 35.0,
          height: 35.0,),
        title: Text(
          text,
          style: TextStyle(
            color: _colorMatching(layoutSelection),
          ),
        )
    );
  }

  void _onLayoutSelection(LayoutType type){
    setState(() {
      _layoutSelection = type;
    });
  }

  void _onSelectTab(int index){
      _controller.jumpToPage(index);
      switch(index){
        case 0:{
          _onLayoutSelection(LayoutType.mession);
        }
        break;
        case 1:{
          _onLayoutSelection(LayoutType.goods);
        }
        break;
        case 2:{
          _onLayoutSelection(LayoutType.reward);
        }
        break;
        case 3:{
          _onLayoutSelection(LayoutType.mine);
        }
        break;
      }
  }
}
